const reqlib = require('app-root-path').require
const numberUtil = reqlib('/app/util/numberUtil')

function kdjUpDays (list, days) {
  if (list.length <= days) {
    return false
  }
  let last = list.length - 1
  for (let i = 0; i < days; i++) {
    const em = list[last - i]
    const diffJ = em.kdj.J - em.kdj.D
    if (diffJ < 0) {
      return false
    }
  }
  return true
}

function highWaveDays (emaList, days) {
  if (emaList.length <= days) {
    return 0
  }
  let r = 0
  let last = emaList.length - 1
  for (let i = 0; i < days; i++) {
    const em = emaList[last - i]
    const kline = em.kline
    const hl = numberUtil.countDifferenceRate(kline.high, kline.low)
    const oc = numberUtil.countDifferenceRate(kline.open, kline.close)
    const res = Math.abs(hl / oc)
    r += (res > 5 ? 5 : res)
  }
  return r / days
}

function maxDiffDays (emaList, days) {
  if (emaList.length <= days) {
    return 0
  }
  let r = 0
  let last = emaList.length - 1
  for (let i = 0; i < days; i++) {
    const em = emaList[last - i]
    r += em.emaMaxDiff
  }
  return r / days
}

// function em510UpDays (list, days) {
//   if (list.length <= days) {
//     return false
//   }
//   let last = list.length - 1
//   for (let i = 0; i < days; i++) {
//     const em = list[last - i]
//     const diffJ = em.ema5 - em.ema10
//     if (diffJ < 0) {
//       return false
//     }
//   }
//   return true
// }

// function kdjDownDays(list, days) {
//   if (list.length <= days) {
//     return false
//   }
//   let last = list.length - 1
//   for (let i = 0; i < days; i++) {
//     const em = list[last - i]
//     const diffJ = em.kdj.J - em.kdj.D
//     if (diffJ > 0) {
//       return false
//     }
//   }
//   return true
// }

exports.buySellFlag = function (btbItem, btbOpInfoJson) {
  const btbOpInfo = btbOpInfoJson[btbItem.code] || {}
  btbOpInfo.actionStatus = btbOpInfo.actionStatus || ''
  const em = btbItem.asInfo[0]
  const emtr = btbItem.hourEma510 || 0
  const hourEmaInfo = btbItem.hourEmaInfo || {}
  const emKdj = hourEmaInfo.kdj ? (hourEmaInfo.kdj.J - hourEmaInfo.kdj.D) : 0
  const emEm = numberUtil.countDifferenceRate(hourEmaInfo.ema5 || 0, hourEmaInfo.ema10 || 1)
  let hourEma510 = -1
  let isBad = false
  if (hourEmaInfo) {
    if (emEm > 0 || emtr > 0 || hourEmaInfo.macd > 0 || emKdj > 2) {
      hourEma510 = 1
    }
    if (hourEmaInfo.ema5 < hourEmaInfo.ema10 && hourEmaInfo.ema10 < hourEmaInfo.ema20 && (emtr < 0 || hourEmaInfo.macd < 0)) {
      hourEma510 = -1
      isBad = true
    }
  }
  const diffJ = em.kdj ? (em.kdj.J - em.kdj.D) : 0
  const asList = btbItem.asList
  const highWave = highWaveDays(asList, 6)
  const diffRateMonth = numberUtil.countDifferenceRate(em.ema5, em.ema10)
  // const emaMaxDiff = em.emaMaxDiff || 0
  const emaMaxDiffAV = maxDiffDays(asList, 2)
  // console.log(emaMaxDiffAV)
  const emaMaxDiffE = emaMaxDiffAV > 0.4
  const f = 3
  let buy = false
  let sell = false
  let buyRate = 1
  let sellRate = 1
  if (em.tr > 0) {
    let buyFlag = true
    if (kdjUpDays(asList, 9) && diffRateMonth > 0.35) {
      btbOpInfo.actionStatus = 'sell-kdj'
      // 可以买，不买就追不上了
      // buyFlag = false
    }
    if (btbOpInfo.actionStatus === 'sell-kdj') {
      if (diffJ < 2) {
        buyFlag = false
      }
    }
    if (btbOpInfo.actionStatus === 'sell-kdj-done') {
      if (diffJ < 2) {
        buyFlag = false
      } else {
        btbOpInfo.actionStatus = ''
      }
    }
    if (hourEma510 > 0) {
      if (buyFlag && emaMaxDiffAV > 0.3) {
        if (diffJ >= 2) {
          buy = true
        }
      }
    } else {
      if (diffJ >= 2) {
        // 小时线需要在上
        if (highWave < f && emaMaxDiffAV > 0.3) {
          if (em.ema5 > em.ema10 && em.macd > 0) {
            buy = true
            buyRate = hourEma510 > 0 ? 1 : 0.5
          }
        }
        // buy = true
        // buyRate = hourEma510 > 0 ? 1 : 0.5
      } else {
        // 小时下
        if (isBad) {
          if (highWave < f || (em.ema5 < em.ema20) || (em.ema10 < em.ema20) || em.macd < 0) {
            sell = true
            if (btbOpInfo.actionStatus === 'sell-kdj') {
              btbOpInfo.actionStatus = 'sell-kdj-done'
            } else {
              btbOpInfo.actionStatus = ''
            }
          }
        } else {
          if (highWave < f && (em.ema5 > em.ema10 && hourEma510 < 0) && emaMaxDiffE) {
            sell = true
            if (btbOpInfo.actionStatus === 'sell-kdj') {
              btbOpInfo.actionStatus = 'sell-kdj-done'
            } else {
              btbOpInfo.actionStatus = ''
            }
          }
        }
      }
    }
  } else {
    // sell = true
    // btbOpInfo.actionStatus = ''
    if (diffJ >= 2) {
      if (((em.ema10 > em.ema20 && em.ema5 > em.ema20) || hourEma510 > 0) && emaMaxDiffE) {
        buy = true
        buyRate = 1
      }
    } else {
      if (hourEma510 < 0) {
        sell = true
        btbOpInfo.actionStatus = ''
      } else {
        if (em.macd < 0 && em.ema5 < em.ema10) {
          sell = true
          btbOpInfo.actionStatus = ''
        }
      }
    }
  }
  if (diffJ < 2) {
    if (btbOpInfo.actionStatus === 'sell-kdj') {
      if (hourEma510 > 0 && (em.ema10 > em.ema20 && em.ema5 > em.ema10) && em.tr > 0 && em.macd > 0) {
      } else {
        btbOpInfo.actionStatus = 'sell-kdj-done'
        sell = true
      }
    }
  } else {
    if (btbOpInfo.actionStatus === 'sell-kdj-done') {
      btbOpInfo.actionStatus = ''
    }
  }
  if (em.kdj && em.kdj.J > em.kdj.D && em.kdj.J >= (em.kdj.D * 2) && hourEma510 > 0 && em.macd > 0) {
    buy = true
    buyRate = 1
  }
  btbOpInfoJson[btbItem.code] = btbOpInfo
  return {
    buy,
    buyRate,
    sell,
    sellRate
  }
}

// tr&&em5>em10>em20
exports.buySell3Flag = function (btbItem, btbOpInfoJson) {
  const btbOpInfo = btbOpInfoJson[btbItem.code] || {}
  btbOpInfo.actionStatus = ''
  const em = btbItem.asInfo[0]
  let buy = false
  let sell = false
  if (em.tr > 0) {
    if (em.ema5 > em.ema10 && em.ema10 > em.ema20) {
      buy = true
    }
  } else {
    sell = true
  }
  btbOpInfoJson[btbItem.code] = btbOpInfo
  return {
    buy,
    sell
  }
}

// em5>em10
exports.buySell1Flag = function (btbItem, btbOpInfoJson) {
  const btbOpInfo = btbOpInfoJson[btbItem.code] || {}
  btbOpInfo.actionStatus = ''
  const em = btbItem.asInfo[0]
  let buy = false
  let sell = false
  if (em.ema5 > em.ema10) {
    if (em.ema10 > em.ema20) {
      buy = true
    }
  } else {
    sell = true
  }
  btbOpInfoJson[btbItem.code] = btbOpInfo
  return {
    buy,
    sell
  }
}

// macd>0
exports.buySell2Flag = function (btbItem, btbOpInfoJson) {
  const btbOpInfo = btbOpInfoJson[btbItem.code] || {}
  btbOpInfo.actionStatus = ''
  const em = btbItem.asInfo[0]
  let buy = false
  let sell = false
  if (em.macd > 0) {
    buy = true
  } else {
    sell = true
  }
  btbOpInfoJson[btbItem.code] = btbOpInfo
  return {
    buy,
    sell
  }
}

// kdj
exports.buySell5Flag = function (btbItem, btbOpInfoJson) {
  const btbOpInfo = btbOpInfoJson[btbItem.code] || {}
  btbOpInfo.actionStatus = ''
  const em = btbItem.asInfo[0]
  const diffJ = em.kdj.J - em.kdj.D
  let buy = false
  let sell = false
  if (diffJ >= 2) {
    buy = true
  } else {
    sell = true
  }
  btbOpInfoJson[btbItem.code] = btbOpInfo
  return {
    buy,
    sell
  }
}
